package db

import (
	"gorm.io/gorm"
	"time"
)

// 任务状态,任务历史
type JobStatus struct {
	gorm.Model
	ServerName string   			`gorm:"column:server_name;index"`
	Status 	string					`gorm:"column:status"`			//任务的状态
	User 	string					`gorm:"column:user"`			//触发任务的用户
	Description string				`gorm:"column:description"`		// 备注
	StartTime	time.Time			`gorm:"column:start_time"`
	EndTime time.Time				`gorm:"column:end_time"`
	//Instances string    			`gorm:"column:instances"`		//执行的主机对应的tag 或者 一堆主机名（主机名具有唯一性）
	InstanceType string 			`gorm:"column:instance_type"`	// tag/list
	InstanceCount int				`gorm:"column:instance_count"`  // 任务总共主机数量
	JobDefinition  string			`gorm:"column:job_definition;type:varchar(1000)"` 	// 任务具体的内容
	Timeout	  int 					`gorm:"column:timeout"`
	GroupInstances string 			`gorm:"column:group_instances;type:text"`  // example 1:dmcu1,dmcu2;2:dmcu3,dmcu4
	JobPolicy	int 				`gorm:"column:job_policy"`
	OkCount int 					`gorm:"column:ok_count"`
	Version string					`gorm:"column:version"`
	RpmName string					`gorm:"column:rpm_name"`
}


// 任务执行详情+日志
type JobInstance struct {
	gorm.Model
	//Id               int64
	JobId   uint 			`gorm:"column:job_id;index"`
	Status 	string			`gorm:"column:status;index"`
	JobLog  string			`gorm:"column:job_log;type:varchar(2000)"`			// 执行的日志
	StartTime	time.Time	`gorm:"column:start_time"`		// 发送给client的时间
	EndTime time.Time		`gorm:"column:end_time"`		// client执行结束上报的内容
	Instance  string 		`gorm:"column:instance;index"`		// 主机名
	Group    int 			`gorm:"column:group"`			//1
}
// 任务模板
type JobModel struct {
	gorm.Model
	ModelName	string	`gorm:"column:model_name;index"`
	Context  	string	`gorm:"column:context;type:varchar(500)"`
	User 		string	`gorm:"column:user"`
	UpdateUser 	string	`gorm:"column:update_user"`
	ServerName 	string  `gorm:"column:server_name;index"`
	Timeout	int     `gorm:"column:timeout"`
}


// 应用代码包和版本
type Rpm struct {
	gorm.Model
	ServerName string   `gorm:"column:server_name;index"`
	RpmName string		`gorm:"cloumn:rpm_name"`
	Url     string		`gorm:"cloumn:url"`
	Version string		`gorm:"column:version"`
	User    string		`gorm:"column:user"`
	Md5 	string 		`gorm:"cloumn:md5"`
	Latest	bool 		`gorm:"cloumn:latest"`
}

// 主机列表
type Instance struct {
	gorm.Model
	IsOnline    bool   	// 是否在线
	Ip 			string
	HostName 	string 		`json:"host_name"`
	AgentVersion float64 	`json:"agent_version"`
	HardwareAddr string  	`json:"hardware_addr" 		gorm:"key"`  // 网卡 ,惟一索引
	System 		string		`json:"system"`
	SystemVersion string 	`json:"system_version"`
	SystemPlatform string 	`json:"system_platform"`
	Kernel 	string			`json:"kernel"`
	Groups []Group	`gorm:"many2many:group_instance"`
}

// 服务器分组
type Group struct {
	gorm.Model
	GroupName string
	ServerName string		`gorm:"cloumn:server_name"`
	Description string		`gorm:"cloumn:description"`
	User string
	Instances []Instance	`gorm:"many2many:group_instance"`
}

type UploadLog struct {
	gorm.Model  `json:"gorm_model"`
	FileName    string    `gorm:"cloumn:file_name" json:"file_name,omitempty"`
	Url         string    `gorm:"cloumn:url" json:"url,omitempty"`
	LastModTime time.Time `gorm:"column:last_mod_time" json:"last_mod_time"`
	Hostname    string    `gorm:"column:hostname" json:"hostname,omitempty"`
	Md5         string    `gorm:"cloumn:md5" json:"md_5,omitempty"`
	Status      string    `gorm:"cloumn:status" json:"status,omitempty"` // 上传的状态是什么
	Description 	string 		`gorm:"cloumn:description" json:"description,omitempty"`
}
